package com.bootdo.contract.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.contract.domain.TikuDO;
import com.bootdo.contract.service.TikuService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author yang
 * @email 2795210596@qq.com
 * @date 2019-09-04 15:02:25
 */

@Controller
@RequestMapping("/contract/tiku")
public class TikuController {
    @Autowired
    private TikuService tikuService;

    @GetMapping()
    @RequiresPermissions("contract:tiku:tiku")
    String Tiku() {
        return "contract/tiku/tiku";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("contract:tiku:tiku")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<TikuDO> tikuList = tikuService.list(query);
        int total = tikuService.count(query);
        PageUtils pageUtils = new PageUtils(tikuList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("contract:tiku:add")
    String add() {
        return "contract/tiku/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("contract:tiku:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        TikuDO tiku = tikuService.get(id);
        model.addAttribute("tiku", tiku);
        return "contract/tiku/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("contract:tiku:add")
    public R save(TikuDO tiku, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String yuid = (String) session.getAttribute("yuid");
        tiku.setYuid(Integer.parseInt(yuid));
        if (tikuService.save(tiku) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("contract:tiku:edit")
    public R update(TikuDO tiku) {
        tikuService.update(tiku);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("contract:tiku:remove")
    public R remove(Integer id) {
        if (tikuService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("contract:tiku:batchRemove")
    public R remove(@RequestParam("ids[]") Integer[] ids) {
        tikuService.batchRemove(ids);
        return R.ok();
    }

}
